package client.view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class RegisterGui extends JPanel {
	
	private GuiManager manager;
	private JLabel lblInfo;
	private JLabel lblName;
	private JLabel lblPassword;
	private JLabel lblEmail;
	private JLabel lblFeedback;
	
	private JTextField txtName;
	private JTextField txtEmail;
	private JPasswordField txtPassword;

	private JButton btnBack;
	private JButton btnRegister;	

	public RegisterGui(GuiManager manager) {
		this.manager = manager;
		
		setSize(585, 365);
		setLocation(0, 0);
		setVisible(true);
		setLayout(null);

		initGUI();
	}

	private void initGUI() {

		lblInfo = new JLabel();
		lblInfo.setText("Registrer");
		lblInfo.setBounds(31, 12, 300, 20);
		lblInfo.setFont(new java.awt.Font("Segoe UI", 1, 14));
		lblName = new JLabel("Name :");
		lblName.setBounds(31, 44, 100, 22);
		lblEmail = new JLabel("Email :");
		lblEmail.setBounds(31, 72, 100, 22);
		lblPassword = new JLabel("Password :");
		lblPassword.setBounds(31, 100, 100, 22);
		lblFeedback = new JLabel();
		lblFeedback.setBounds(31, 128, 341, 22);
		lblFeedback.setForeground(Color.red);

		
		txtName = new JTextField();
		txtName.setBounds(131, 44, 118, 22);
		txtEmail = new JTextField();		
		txtEmail.setBounds(131, 72, 118, 22);
		txtPassword = new JPasswordField();		
		txtPassword.setBounds(131, 100, 118, 22);

		btnRegister = new JButton("Register");
		btnRegister.setBounds(129, 156, 120, 22);
		btnRegister.addActionListener(new btnRegisterListener());
		btnBack = new JButton("<");
		btnBack.setBounds(31, 156, 50, 22);
		btnBack.addActionListener(new btnBackListener());
		
		
		add(lblInfo);
		add(lblName);
		add(lblEmail);
		add(lblPassword);
		add(lblFeedback);
		
		add(txtName);
		add(txtEmail);
		add(txtPassword);

		add(btnRegister);		
		add(btnBack);	
		
		repaint();
	}
	
	private class btnRegisterListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if (txtName.getText().matches("")){
				lblFeedback.setText("Username is empty");
				return;
			}
			if (txtEmail.getText().matches("")){
				lblFeedback.setText("Email is empty");
				return;
			}
				
			
			char[] chars = txtPassword.getPassword();
			
			if (chars.length == 0){
				lblFeedback.setText("Password is empty");
				return;
			}
			
			String password = "";
			for (int i=0; i<chars.length; i++){
				password+=chars[i];
			}
			
			try {
				if (manager.getAdmin().registerUser(txtName.getText(), password, txtEmail.getText())){
					manager.switchScreen("loginGui");
				}else{
					
				}
			
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	private class btnBackListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("mainGui");
		}
		
	}
	
	public void reset(){
		lblFeedback.setText("");
	}
	
	
}
